﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_DanhSachDoanVienDangKyRenLuyen : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MemberShip_ASP.BindDrop(drldonvi, this.Page.User.Identity.Name);
        }
    }
    protected void btntruyvan_Click(object sender, EventArgs e)
    {
        BindGrid(int.Parse(txtnamrenluen.Text));
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        _dbDataContext _db = new _dbDataContext();
        tblRenLuyenDoanVien val;
        int id;
        int ketquahoanthanh;
        foreach (GridViewRow dr in grid_renluyendoanvien.Rows)
        {
            id = int.Parse(grid_renluyendoanvien.DataKeys[dr.RowIndex].Value.ToString());
            ketquahoanthanh = int.Parse(((DropDownList)dr.FindControl("drlmucdohoanthanh")).SelectedValue.ToString());
            val = _db.tblRenLuyenDoanViens.Single(r => r.ID == id);
            val.MucDoHoanThanh = ketquahoanthanh;
        }
        _db.SubmitChanges();
        lblMessage.Text = "Đã lưu kết quả";
    }
    private void BindGrid(int nam)
    {
        _dbDataContext _db = new _dbDataContext();
        string madonvi = MemberShip_ASP.GetMaDonViFromUserName(this.Page.User.Identity.Name);
        var val = from p in _db.tblRenLuyenDoanViens.Where(r=>r.NamRenLuyen==nam && r.tblDoanVien.TrucThuoc.Trim()==madonvi)
                  select new
                  {
                      ID=p.ID,
                      MaDoanVien = p.MaDoanVien.Trim(),
                      TenDoanVien = p.tblDoanVien.HoTen.Trim(),
                      NgayVaoDoan = string.Format("{0:dd/MM/yyyy}", p.tblDoanVien.NgayVaoDoan),
                      Phai = (p.tblDoanVien.Phai == true ? "Nam" : "Nữ"),
                      NgaySinh = string.Format("{0:dd/MM/yyyy}", p.tblDoanVien.NgaySinh),
                      CMND = p.tblDoanVien.CMND.Trim(),
                      MucDoHoanThanh = p.MucDoHoanThanh
                  };
        if (val.Count() > 0)
        {
            grid_renluyendoanvien.DataSource = val;
            grid_renluyendoanvien.DataBind();
            lblmessage1.Text = "DANH SÁCH ĐOÀN VIÊN";
        }
        else
        {
            lblmessage1.Text = "Không có danh sách đoàn viên đăng ký rèn luyện trong năm " + txtnamrenluen.Text;
            grid_renluyendoanvien.DataSource = null;
            grid_renluyendoanvien.DataBind();
        }
    }
    protected void txtnamrenluen_TextChanged(object sender, EventArgs e)
    {
        //BindGrid(int.Parse(txtnamrenluen.Text));
    }
}